package com.qianwang.shopmsg.service.sign.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qianwang.shopmsg.common.Page;
import com.qianwang.shopmsg.common.StudentStatusEnum;
import com.qianwang.shopmsg.entity.StudentInfo;
import com.qianwang.shopmsg.entity.StudentSign;
import com.qianwang.shopmsg.entity.view.StudentSignView;
import com.qianwang.shopmsg.exception.ParamValidateException;
import com.qianwang.shopmsg.param.StudentSignCreateParam;
import com.qianwang.shopmsg.repository.StudentMsgDao;
import com.qianwang.shopmsg.service.sign.StudentMsgService;
import com.qianwang.shopmsg.util.DateUtil;
import com.qianwang.shopmsg.util.OvalUtil;

@Service("studentMsgService")
public class StudentMsgServiceImpl implements StudentMsgService {

	@Override
	public int countStudentSingInfos(String startTime, String endTime, String studentName) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("isDelete", 0);
		if(StringUtils.isNotBlank(startTime)){
			param.put("startTime", startTime);
		}
		if(StringUtils.isNotBlank(endTime)){
			param.put("endTime", endTime);
		}
		if(StringUtils.isNotBlank(studentName)){
			param.put("studentName", studentName);
		}
		return studentMsgDao.countStudentSingInfos(param);
	}
	
	@Resource(name="studentMsgDao")
	private StudentMsgDao studentMsgDao;
	
	@Override
	public void addStudentMsgInfo(StudentInfo studentInfo) {
		studentMsgDao.addStudentInfo(studentInfo);
	}
	
	@Override
	public List<StudentSign> listStudentSignInfos(Page pg,String startTime, String endTime, String studentName) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("isDelete", 0);
		param.put("pageSize", pg.getPageSize());  
		param.put("pageIndex", pg.getPageIndex());
		if(StringUtils.isNotBlank(startTime)){
			param.put("startTime", startTime);
		}
		if(StringUtils.isNotBlank(endTime)){
			param.put("endTime", endTime);
		}
		if(StringUtils.isNotBlank(studentName)){
			param.put("studentName", studentName);
		}
		return studentMsgDao.listStudentSignInfos(param);
	}
	
	@Override
	@Transactional(rollbackFor=Throwable.class)
	public void saveStudentSign(StudentSignCreateParam signParam) throws ParamValidateException {
		
		OvalUtil.validate(signParam);
		
		StudentSign signOld = studentMsgDao.loadStudentSignById(signParam.getSignId());
		
		if(signOld != null){
			updateStudentSign(signParam,signOld);
		}else{
			createStudentSign(signParam);
		}
	}
	
	/**
	 * update操作
	 * @param signParam
	 * @param studentInfo 
	 */
	private void updateStudentSign(StudentSignCreateParam signParam,StudentSign signOld){
		BeanUtils.copyProperties(signParam, signOld);
		signOld.setStudentName(signParam.getName());
		
		StudentInfo studentInfo = studentMsgDao.loadStudentInfo(signParam.getStudentId());
		BeanUtils.copyProperties(signParam, studentInfo);
		
		//update 学生sign信息
		studentMsgDao.updateStudentSign(signOld);
		//update 学生信息
		studentMsgDao.updateStudentInfo(studentInfo);
	}
	
	/**
	 * create操作
	 * @param signParam
	 */
	private void createStudentSign(StudentSignCreateParam signParam){
		StudentInfo studentInfo = new StudentInfo();
		BeanUtils.copyProperties(signParam, studentInfo);
		
		StudentSign sign = new StudentSign();
		BeanUtils.copyProperties(signParam, sign);
		sign.setSignTime(DateUtil.now());
		sign.setStudentId(studentMsgDao.addStudentInfo(studentInfo));
		sign.setStudentName(signParam.getName());
		studentMsgDao.addStudentSign(sign);
	}
	
	@Override
	public void deleteStudentSign(long id) {
		StudentSign sign = studentMsgDao.loadStudentSignById(id);
		sign.setIsDelete(StudentStatusEnum.DELETED.getCode());
		studentMsgDao.updateStudentSign(sign);
	}
	
	@Override
	public StudentSignView loadSignWithStuInfoById(long id) throws ParamValidateException {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("isDelete", 0);
		param.put("id", id);
		StudentSignView view = studentMsgDao.loadSignWithStuInfoById(param);
		if(null == view){
			throw new ParamValidateException("信息不存在，请稍后重试！");
		}
		return view;
	}
}
